package com.example.projectjava.controller;


import com.example.projectjava.service.AdminService;
import com.example.projectjava.util.JsonResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@Tag(name = "管理员接口  ")
public class AdminController {

    @Autowired   //此注解的作用：是spring的基本特性，它将项目中运行过程中的JdbcTemplate,自动创建并实例化传入
    JdbcTemplate jdbcTemplate;

    @Autowired
    AdminService adminService;

    /**
     * 管理员登录的方法
     *
     * @param adminname
     * @param pwd
     * @return //?userName=manager&password=123456
     */
    @ResponseBody
    @GetMapping("/admin_login")

    @Operation(summary = "管理员登录")
    @Parameters({
            @Parameter(name ="adminname",description="用户名(不允许为空)",in = ParameterIn.QUERY,required = false),
            @Parameter(name ="pwd",description="密码(不允许为空)",in = ParameterIn.QUERY,required = true)
    })
    public JsonResponse adminLogin(String adminname, String pwd) {
        JsonResponse jsonResponse = new JsonResponse();
        try {
            adminService.adminLogin(adminname, pwd);    //调用service层的userLogin方法判断用户名密码是否正确
            jsonResponse.setCode(0);
            jsonResponse.setMsg("登录成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResponse.setCode(1);
            jsonResponse.setMsg("登录失败:" + e.getMessage());
        }
        return jsonResponse;
    }
}
